﻿using System;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace CRC.NHibernate {
    public sealed class NHibernateHelper
    {
        private static readonly ISessionFactory sessionFactory = null;
        private static ISession currentSession  = null;

        static NHibernateHelper()
        {
            Configuration cfg = new Configuration().Configure();
            cfg.AddAssembly("NHibernateMappings");
            sessionFactory = cfg.BuildSessionFactory();
            
        }

        public static ISession GetCurrentSession()
        {
            if (currentSession == null || !currentSession.IsOpen)
            {
                currentSession = sessionFactory.OpenSession();
            }
            return currentSession;
        }

        public static void CloseSession()
        {

            if (currentSession == null)
            {
                // No current session
                return;
            }
            currentSession.Close();
        }

        public static void CloseSessionFactory()
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }
}
